<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2019-01-22
 * Time: 10:31
 */

namespace App\Http\Controllers\Api\V1\Manager\Mall;



use App\Http\Controllers\Controller;
use App\Models\GiftCommodity;
use App\Models\GiftOrder;
use App\Models\GiftOrderDetail;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GiftOrderController extends Controller
{
    public function index(Request $request)
    {
        $pageSize = $request->pageSize?$request->pageSize:20;
        $orderStatus = $request->orderStatus;
        $keyWords = $request->keyWords;
        $sql = GiftOrder::select('t1.*','t2.nickName')
            ->from((new GiftOrder())->getTable().' as t1')
            ->join((new User())->getTable().' as t2','t1.userId','=','t2.userId');
        if($orderStatus){
            $sql->where('t1.orderStatus',$orderStatus);
        }
        if ($keyWords) {
            $sql->where(function ($query) use ($keyWords) {
                $query->where('t2.nickName', 'like', '%' . $keyWords . '%')
                    ->orWhere(function ($query) use ($keyWords) {
                        $query->where('t1.phone', 'like', '%' . $keyWords . '%')
                            ->orWhere(function ($query) use ($keyWords) {
                                $query->where('t1.orderNo', 'like', '%' . $keyWords . '%');
                            });
                    });
            });
        }
        $data = $sql->orderBy('t1.createTime','desc')->paginate($pageSize)->toArray();
        $result['orderList'] = $data['data'];
        $result['totalNum'] = $data['total'];
        return $this->apiResponse($result);
    }

    public function show($orderNo, Request $request)
    {
            $data = GiftOrder::where('orderNo', $orderNo)->with('orderDetail')->first();
        return $this->apiResponse($data);
    }

    public function store(Request $request)
    {
        $data = $request->only(['orderNo', 'reciverName', 'phone', 'province', 'city', 'area', 'detailAddress']);
        $validator = Validator::make($data, [
            'orderNo' => 'required',
            'reciverName' => 'required',
            'phone' => 'required',
            'province' => 'required',
            'city' => 'required',
            'area' => 'required',
            'detailAddress' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->apiResponse('', config('errorCode.INVALID_REQ_PARAMS'));
        }
        $data['orderStatus'] = 'WAITDELIVER';
        GiftOrder::where(['orderNo' => $data['orderNo'], 'orderStatus' => 'WAITING'])->update($data);
        return $this->apiResponse(true);
    }


    public function deliver(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make($data, [
            'orderNo'   =>'required',
            'deliverName'   =>'required',
            'deliverNo'   =>'required',
        ]);
        if($validator->fails()) {
            return $this->apiResponse('', config('errorCode.INVALID_REQ_PARAMS'));
        }
        $status = GiftOrder::where(['orderNo'=>$data['orderNo'],'orderStatus'=>'ORDERFINISH'])
            ->update(['orderStatus'=>'WAITRECEIVE','deliverName'=>$data['deliverName'],
                'deliverNo'=>$data['deliverNo'],
                'deliveryTime'=>date("Y-m-d H:i:s",time())]);
        if(!$status){
            return $this->apiResponse('', config('errorCode.DELIVER_FAILED'));
        }else{
            return $this->apiResponse(true);
        }
    }
}